#include <iostream>
#include <string.h>
#include "QuoteHandler.h"
#include "StrategyInterface.h"
using namespace std;

QuoteHandler::QuoteHandler(CSHZdMarketApi *api) {
	m_api = api;
	m_api->RegisterSpi(this);

	m_api->Init();
	m_api->AuthonInfo((char*)"55822DC39D9316D5111D9EED00C1CED81B6F0DCEA8D97DDEBD350D939CF8A9D304E3C73A742CFB80");
	m_api->RegisterLoginFront((char*)"protocol://222.73.119.230:7003");
	m_api->RegisterFront((char*)"protocol://222.73.105.170:9002");//
}

QuoteHandler::~QuoteHandler()
{
}

void QuoteHandler::OnFrontConnected()
{
	cout << "Info connected sucessed!" << endl;
	CTShZdReqUserLoginField field = { 0 };
	memcpy(field.UserID, "MN003849", 16);
	memcpy(field.Password, "888888", 41);
	m_api->ReqUserLogin(&field, 1);
}

void QuoteHandler::OnFrontDisconnected(int nReason)
{
	cout << "Info Disconnected!" << nReason << endl;
}

void QuoteHandler::OnHeartBeatWarning(int nTimeLapse)
{

}

void QuoteHandler::OnRspUserLogin(CTShZdRspUserLoginField *pRspUserLogin, CTShZdRspInfoField *pRspInfo,
	int nRequestID, bool bIsLast)
{
	cout << "login:" << pRspUserLogin->UserID << " " << pRspUserLogin->AccountID << " " << pRspUserLogin->CurrencyNo << endl;


}

void QuoteHandler::OnRspUserLogout(CTShZdUserLogoutField *pUserLogout, CTShZdRspInfoField *pRspInfo,
	int nRequestID, bool bIsLast)
{
	cout << "BrokerId: " << pUserLogout->BrokerID<<" UserId: "<< pUserLogout->UserID << endl;

}

void QuoteHandler::OnRspError(CTShZdRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
	cout << "error:" << pRspInfo->ErrorID << "===" << pRspInfo->ErrorMsg << endl;
}
void QuoteHandler::OnRspSubMarketData(CTShZdSpecificInstrumentField *pSpecificInstrument, CTShZdRspInfoField *pRspInfo,
	int nRequestID, bool bIsLast)
{
	cout << "SubMarketData: " << pSpecificInstrument->InstrumentID << endl;
}
void QuoteHandler::OnRspUnSubMarketData(CTShZdSpecificInstrumentField *pSpecificInstrument, CTShZdRspInfoField *pRspInfo,
	int nRequestID, bool bIsLast)
{

}
void QuoteHandler::OnRtnDepthMarketData(CTShZdDepthMarketDataField *pDepthMarketData)
{
	cout << "M:" << pDepthMarketData->ExchangeID << " " << pDepthMarketData->InstrumentID << " "
		<< pDepthMarketData->AskPrice1 << " " << pDepthMarketData->BidPrice1 << " " << pDepthMarketData->UpdateTime << endl;
	OnDepthMarketData_(pDepthMarketData);
}

void QuoteHandler::OnRtnFilledMarketData(CTShZdFilledDataField* pFilledMarketData)
{
	cout << "F:" << pFilledMarketData->ExchangeID << " " << pFilledMarketData->InstrumentID << " "
		<< pFilledMarketData->Volume << " " << pFilledMarketData->LastPrice << " " << pFilledMarketData->FilledVolume
		<< " " << pFilledMarketData->UpdateTime << endl;
	OnRtnFilledMarketData_(pFilledMarketData);
}

